﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Web;

namespace B4Deal.Models
{
    public class DealManager
    {
        private B4Deal_DataEntities localDB;
        public  List<Deal> lstDeal = new List<Deal>();
        public DealManager()
        {
            localDB = B4DealDatabase.b4Database;
            Deal deal = new Deal();
            lstDeal = deal.getAllDeal();
        }
        public bool DealExists(KHUYENMAI km)
        {
            if (km == null)
                return false;
            return (localDB.KHUYENMAIs.SingleOrDefault(k => k.MaKM == km.MaKM) != null);
        }
        //public List<Deal> GetAllDeal()
        //{
        //    List<Deal> deals = new List<Deal>();
        //    using (var db = new B4Deal_DataEntities())
        //    {
        //        var temp = (from km in localDB.KHUYENMAIs
        //                    orderby km.MaKM
        //                    where km.TinhTrang != -1
        //                    select km).ToList();
        //        foreach (var deal in temp)
        //            deals.Add(new Deal(deal.MaKM, Convert.ToInt32(deal.LoaiKM), deal.TenKM, Convert.ToInt32(deal.DonGiaCu), Convert.ToInt32(deal.DonGiaMoi), Convert.ToDateTime(deal.BatDauKM), Convert.ToDateTime(deal.KetThucKM),
        //                               deal.MoTa, deal.DiemNoiBat, deal.DKSuDung, deal.GioiThieuChiTiet,
        //                               Convert.ToInt32(deal.DoanhNghiep), deal.HinhAnh, deal.ClipQuangCao, Convert.ToInt32(deal.HinhThucGiaoSP), Convert.ToInt32(deal.TinhTrang), deal.DONHANGs.Count, 1000));
        //    }
        //    return deals;
        //}
        public List<KHUYENMAI> GetAllDeal()
        {
            List<KHUYENMAI> lst = (from km in localDB.KHUYENMAIs
                                   orderby km.MaKM
                                   where km.TinhTrang != -1
                                   select km).ToList();
            return lst;
        }
        public void Update(KHUYENMAI dealToUpdate)
        {
             KHUYENMAI km = GetDeal(dealToUpdate.MaKM);
             CloneKM(km,dealToUpdate);
            try
            {
                localDB.ApplyOriginalValues(km.EntityKey.EntitySetName,dealToUpdate);
                Save();  
            }catch(Exception e)
            {
            }

            
        }
      
        public void Create(Deal dealToUpdate)
        {
          
            KHUYENMAI km = new KHUYENMAI();
            ConvertoKM(km, dealToUpdate);
            DOANHNGHIEP dn = new DOANHNGHIEP();
            
            km.DOANHNGHIEP1 = dn; 
            try
            {
                localDB.AddToKHUYENMAIs(km);
            }
            catch (ArgumentException ae)
            {
                throw ae;
            }
            Save();
        }
        public void Save()
        {
            localDB.SaveChanges();
        }
        public void ConvertoKM(KHUYENMAI km, Deal dealToUpdate)
        {
            km.MaKM = dealToUpdate.MaKM;
            km.TenKM = dealToUpdate.TenKM;
            km.LoaiKM = dealToUpdate.LoaiKM;
            km.HinhAnh = dealToUpdate.HinhAnh;
            km.MoTa = dealToUpdate.MoTa;
            km.GioiThieuChiTiet = dealToUpdate.GioiThieuChiTiet;
            km.KetThucKM = dealToUpdate.KetThucKM;
            km.BatDauKM = dealToUpdate.BatDauKM;
            km.HinhThucGiaoSP = dealToUpdate.HinhThucGiaoSP;
            km.TinhTrang = dealToUpdate.TinhTrang;
            km.DoanhNghiep = dealToUpdate.MaDN;
            km.DKSuDung = dealToUpdate.DkSuDung;
            km.DonGiaCu = dealToUpdate.DonGiaCu;
            km.DonGiaMoi = dealToUpdate.DonGiaMoi;
            km.DiemNoiBat = dealToUpdate.DacDiemNoiBat;
           
        }
        public void CloneKM(KHUYENMAI km, KHUYENMAI dealToUpdate)
        {
            km.MaKM = dealToUpdate.MaKM;
            km.TenKM = dealToUpdate.TenKM;
            km.LoaiKM = dealToUpdate.LoaiKM;
            km.HinhAnh = dealToUpdate.HinhAnh;
            km.MoTa = dealToUpdate.MoTa;
            km.GioiThieuChiTiet = dealToUpdate.GioiThieuChiTiet;
            km.KetThucKM = dealToUpdate.KetThucKM;
            km.BatDauKM = dealToUpdate.BatDauKM;
            km.HinhThucGiaoSP = dealToUpdate.HinhThucGiaoSP;
            km.TinhTrang = dealToUpdate.TinhTrang;
            km.DoanhNghiep = dealToUpdate.DoanhNghiep;
            km.DKSuDung = dealToUpdate.DKSuDung;
            km.DonGiaCu = dealToUpdate.DonGiaCu;
            km.DonGiaMoi = dealToUpdate.DonGiaMoi;
            km.DiemNoiBat = dealToUpdate.DiemNoiBat;

        }

        public void Remove(KHUYENMAI dealToUpdate)
        {
            KHUYENMAI u = GetDeal(dealToUpdate.MaKM);         
             u.TinhTrang = -1;
           localDB.ApplyOriginalValues(dealToUpdate.EntityKey.EntitySetName,u);
            Save();
        }
 
        public KHUYENMAI GetDeal(int mkm)
        {
            return localDB.KHUYENMAIs.SingleOrDefault(km => km.MaKM == mkm);
        }
    
    }
}